﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 测量大师
{
    public partial class 双定向附合导线 : Form
    {
        //增加一个全局变量数据表
        DataTable dt = new DataTable();
        public DataTable pdt = new DataTable();
        Point[] point;
        string[] PName;
        double[] TPX;
        double[] TPY;
        public 双定向附合导线()
        {
            InitializeComponent();
        }

        private void 选择数据输入模式_Click(object sender, EventArgs e)
        {
            选择数据输入模式.Visible = false;
            手动输入.Visible = true;
            文件读取.Visible = true;

            calculator.CreateFHTabel(dt);
            数据表.DataSource = dt;
        }

        private void 手动输入_Click(object sender, EventArgs e)
        {
            手动输入.Visible = false;
            文件读取.Visible = false;
            添加新的观测点.Visible = true;
           

        }

        private void 添加新的观测点_Click(object sender, EventArgs e)
        {

            dt.Rows.Add();
        }

        private void 文件读取_Click(object sender, EventArgs e)
        {
            try
            {
                string path = string.Empty;
                OpenFileDialog openFileDialog = new OpenFileDialog()
                {
                    Filter = "Files (*.txt)|*.txt"
                };

                //var result = openFileDialog.ShowDialog();
                if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    path = openFileDialog.FileName;
                }
                if (path == "")
                {
                    MessageBox.Show("请选择你要导入的txt文档", "提示");
                }

                StreamReader sr = new StreamReader(path);
                string oneline;
                while (!sr.EndOfStream)
                {
                    oneline = sr.ReadLine(); //逐行读取
                    string[] a = oneline.Split(',');//分割
                    DataRow dr = dt.NewRow();
                    dr[0] = a[0];
                    dr[1] = a[1];
                    dr[2] = a[2];
                    dr[3] = a[3];
                    dr[4] = a[4];
                    dr[5] = a[5];
                    dr[6] = a[6];
                    dr[7] = a[7];
                    dr[8] = a[8];
                    dr[9] = a[9];
                    dr[10] = a[10];

                    dt.Rows.Add(dr);
                    数据表.DataSource = dt;

                }
                sr.Close();
            }
            catch (Exception)
            {

                MessageBox.Show("文件读取失败，请重新打开本页面");
            }
            
        }

        private void 平差_Click(object sender, EventArgs e)
        {
            int judge = 0;
           
                if (右角.Checked == true)
                {
                   
                    judge = 1;
                }
            
          
            
            pdt = dt.Copy();
            try
            {
                double Ssum, DeltasumX, DeltasumY;
                int DeltaAngle;
                measuring_adjustment.SDXFH(dt, pdt,out DeltaAngle , out Ssum , out DeltasumX , out DeltasumY,judge);
                闭合差显示.Text = string.Format("角度闭合差为：{0}秒 ， 应小于50秒\nX方向的闭合差为：{1}\nY方向的闭合差为：{2}\n导线全长闭合差为：{3}\n导线全长相对闭合差为：{4},应小于1/5000", DeltaAngle, DeltasumX, DeltasumY, Math.Sqrt(DeltasumX * DeltasumX + DeltasumY * DeltasumY), Math.Sqrt(DeltasumX * DeltasumX + DeltasumY * DeltasumY) / Ssum);
            }
            catch (Exception)
            {

                MessageBox.Show("数据格式或位置输入错误，请查看帮助文档，内有关于格式输入的介绍！");
            }
            
            数据表.DataSource = pdt;
        }

        private void 绘图_Click(object sender, EventArgs e)
        {
            int rows = pdt.Rows.Count;
            Point[] points = new Point[rows];
            int minX = (int)((double)pdt.Rows[0][9]);
            int minY = (int)((double)pdt.Rows[0][10]);
            double[] tpx = new double[rows];
            double[] tpy = new double[rows];
            for (int i = 0; i < rows; i++)
            {
                tpx[i] = (double)pdt.Rows[i][9];
                tpy[i] = (double)pdt.Rows[i][10];
                if ((int)((double)pdt.Rows[i][9]) < minX)
                    minX = (int)((double)pdt.Rows[i][9]);
                if ((int)((double)pdt.Rows[i][10]) < minY)
                    minY = (int)((double)pdt.Rows[i][10]);

            }
            TPX = tpx;
            TPY = tpy;
            string[] name = new string[rows];
            for (int i = 0; i < rows; i++)
            {
                points[i].X = (int)((double)pdt.Rows[i][9]) + Math.Abs(minX) + 22;
                points[i].Y = (int)((double)pdt.Rows[i][10]) + Math.Abs(minY) + 22;
                name[i] = pdt.Rows[i][0].ToString();
            }
            point = points;
            PName = name;
            calculator.DrawCrosses(point, 导线图.CreateGraphics(), name, tpx, tpy);
            导线图.Visible = true;
        }

        private void 导线图_Paint(object sender, PaintEventArgs e)
        {
            calculator.DrawCrosses(point, e.Graphics, PName, TPX, TPY);
        }
        private void 导线图_MouseWheel(object sender, MouseEventArgs e)
        {
            int x = e.Location.X;
            int y = e.Location.Y;
            int ow = 导线图.Width;
            int oh = 导线图.Height;
            int VX, VY;  //因缩放产生的位移矢量
            if (e.Delta > 0) //放大
            {
                //第①步
                导线图.Width += 12;
                导线图.Height += 12;
                //第②步
                PropertyInfo pInfo = 导线图.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
                 BindingFlags.NonPublic);
                Rectangle rect = (Rectangle)pInfo.GetValue(导线图, null);
                //第③步
                导线图.Width = rect.Width;
                导线图.Height = rect.Height;
            }
            if (e.Delta < 0) //缩小
            {


                导线图.Width -= 12;
                导线图.Height -= 12;
                PropertyInfo pInfo = 导线图.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
                 BindingFlags.NonPublic);
                Rectangle rect = (Rectangle)pInfo.GetValue(导线图, null);
                导线图.Width = rect.Width;
                导线图.Height = rect.Height;
            }
            //第④步，求因缩放产生的位移，进行补偿，实现锚点缩放的效果
            VX = (int)((double)x * (ow - 导线图.Width) / ow);
            VY = (int)((double)y * (oh - 导线图.Height) / oh);
            导线图.Location = new Point(导线图.Location.X + VX, 导线图.Location.Y + VY);
        }

        private void 回到主页ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void 表格格式化ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dt.Clear();
            数据表.DataSource = dt;
        }

        private void 提示样例ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("..\\..\\..\\提示样例\\双定向.png");

        }

        private void 帮助文档ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("..\\..\\..\\帮助文档\\一键平帮助文档.docx");
        }
    }
}
